package com.devshephard.jucks.countDown.addDownLunch;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class cyclicBarroerTest {

    public static void main(String[] args) {

        //定义一个线程加法计数器，计数数量为6
        CyclicBarrier cyclicBarrier  = new CyclicBarrier(7,()->{
            //当线程执行完毕，就会调用下面的输出方法
            System.out.println("线程执行完毕");
        });

        for (int i = 0; i < 7; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() + " Go");
                try {
                    //这里和减法计数器不同，await()方法是写在线程当中的
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }

    }

}
